﻿using DeHeng_Mes.Constant;
using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Dao
{
    public class ProduceDao
    {
        /// <summary>
        /// 判读该产量订单是否存在
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool IsCreate(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionHour>()
                    .Any(x => x.OrderId == orderId);
            }
        }

        /// <summary>
        /// 判断该订单中当前时间段内的产量是否保存
        /// </summary>
        /// <param name="previousHour"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool IsSaveLastTime(string time, string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionHour>()
                    .Any(x => x.OrderId == orderId && x.NowTime == time);
            }
        }

        /// <summary>
        /// 插入每小时产量数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="currentHour"></param>
        /// <param name="lastProduce"></param>
        /// <returns></returns>
        public int InsertProduce(string orderId, string currentHour, string lastProduce, string produceNg, string timeLong)
        {
            using (var db = SqlSugarConfig.Db)
            {
                // 创建一个新的ProductionHour对象
                var productionData = new ProductionHour
                {
                    OrderId = orderId,
                    NowTime = currentHour,
                    Produce = lastProduce,
                    ProduceNg = produceNg,
                    TimeLong = timeLong
                };
                // 插入数据并返回插入的记录数
                return db.Insertable(productionData).ExecuteCommand();
            }
        }

        /// <summary>
        /// 根据单号查询，返回记录数
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<ProductionOperation> GetOperationProduce(string orderId, string time, string operation)
        {
            using (var db = SqlSugarConfig.Db)
            {
                // 查询满足条件的记录数
                return db.Queryable<ProductionOperation>()
                         .Where(x => x.OrderId == orderId && x.TimeInterval == time && x.Operation == operation)
                         .ToList();
            }
        }

        /// <summary>
        /// 根据Id与工序号更新产量数据
        /// </summary>
        /// <param name="produce"></param>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        public int UpdateOperationProduce(string produce, string orderId, string operation)
        {
            using (var db = SqlSugarConfig.Db)
            {
                // 执行更新操作
                int rowsAffected = db.Updateable<ProductionOperation>()
                    .SetColumns(x => x.Produce == produce) // 假设 Produce 是产量数据的字段名
                    .Where(x => x.OrderId == orderId && x.Operation == operation) // 条件：订单ID和工序号
                    .ExecuteCommand();

                // 判断是否有记录被更新
                return rowsAffected;
            }
        }

        /// <summary>
        /// 插入工序小时产量数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        /// <param name="produce"></param>
        /// <param name="time"></param>
        /// <param name="timeLong"></param>
        /// <returns></returns>
        public bool InsertOperation(string orderId, string operation, string produce, string produceNg, string time, string timeLong, string status)
        {
            using (var db = SqlSugarConfig.Db)
            {
                // 创建一个新的ProductionHour对象
                var productionData = new ProductionOperation
                {
                    OrderId = orderId,
                    Operation = operation,
                    Produce = produce,
                    ProduceNg = produceNg,
                    TimeInterval = time,
                    TimeLong = timeLong,
                    Status = status
                };

                // 插入数据并返回插入的记录数
                return db.Insertable(productionData).ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 更改状态为成功状态
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="timeInterval"></param>
        /// <param name="operation"></param>
        public void UpDateStatus(string orderId, string timeInterval, string operation)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<ProductionOperation>()
                    .SetColumns(x => x.Status == SystemConstant.Ok)
                    .Where(x => x.OrderId == orderId && x.TimeInterval == timeInterval && x.Operation == operation)
                    .ExecuteCommand();
            }
        }

        /// <summary>
        /// 查询订单产量列表
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<ProductionHour> QueryProduceTotal(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionHour>()
                         .Where(x => x.OrderId == orderId)
                         .ToList();
            }
        }

        /// <summary>
        /// 根据工序与订单号查询该工序所有的数据
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<ProductionOperation> GetOperationProduceByIdAndOperation(string operation, string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionOperation>()
                         .Where(x => x.OrderId == orderId && x.Operation == operation)
                         .ToList();
            }
        }

        /// <summary>
        /// 删除该订单所有小时产量信息
        /// </summary>
        /// <param name="orderId"></param>
        public void DeleteAllProduceByOrderId(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Deleteable<ProductionOperation>()
                    .Where(x => x.OrderId == orderId)
                    .ExecuteCommand();
            }
        }

        /// <summary>
        /// 查找所有该工单号的报警数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<ProductionOperation> SelectStatusIsNOK(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionOperation>()
                    .Where(x => x.OrderId == orderId && x.Status == SystemConstant.illegal)
                    .ToList();
            }
        }

        /// <summary>
        /// 根据工单与工序
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        public void DeleteOperation(string orderId, string operation)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Deleteable<ProductionOperation>()
                    .Where(x => x.OrderId == orderId && x.Operation == operation)
                    .ExecuteCommand();
            }
        }

        /// <summary>
        /// 获取所有未推送的产量信息
        /// </summary>
        /// <returns></returns>
        public List<ProductionOperation> GetAll()
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionOperation>()
                    .Where(x => x.Status != SystemConstant.Ok)
                    .ToList();
            }
        }

        /// <summary>
        /// 根据订单、工序、时长查询数据库中是否存在该数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        /// <param name="timePeriod"></param>
        /// <returns></returns>
        public bool ProduceIsSave(string orderId, string timePeriod)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<ProductionOperation>()
                    .Where(x => x.OrderId == orderId && x.TimeInterval == timePeriod)
                    .Any();
            }
        }

        /// <summary>
        /// 根据条件更改状态
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        /// <param name="timePeriod"></param>
        /// <param name="illegal"></param>
        public void UpdateOperationStatus(string orderId, string operation, string timePeriod, string illegal)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<ProductionOperation>()
                    .SetColumns(x => x.Status == illegal)
                    .Where(x => x.OrderId == orderId && x.Operation == operation && x.TimeInterval == timePeriod)
                    .ExecuteCommand();
            }
        }
    }
}
